Skip to content

Add LDAP-based git identity and SSSD config for immediate git config setup#274

Open
cmyers-mieweb wants to merge 4 commits intomainfrom
cmyers_wazuh-int
Open

Add LDAP-based git identity and SSSD config for immediate git config setup#274
cmyers-mieweb wants to merge 4 commits intomainfrom
cmyers_wazuh-int

Conversation

@cmyers-mieweb
Copy link
Copy Markdown
Collaborator

Resolves: #256

Installs ldap-utils and adds LDAP client/config and a profile script to auto-configure git user.name/email from LDAP on first interactive login.

Copies ldap.conf to /etc/ldap, adds /etc/profile.d/git-identity.sh which uses ldapsearch and NSS (sssd) gecos to set global git config, and adjusts sssd.conf to map cn -> gecos (ldap_user_gecos = cn).

Also updates Dockerfile to install ldap-utils and include the new files.

This should allow for any user logging into any container to have git config preset and ready to go. This should work on any template derived from the base image.

Install ldap-utils and add LDAP client/config and a profile script to auto-configure git user.name/email from LDAP on first interactive login. Copies ldap.conf to /etc/ldap, adds /etc/profile.d/git-identity.sh which uses ldapsearch and NSS (sssd) gecos to set global git config, and adjusts sssd.conf to map cn -> gecos (ldap_user_gecos = cn). Also updates Dockerfile to install ldap-utils and include the new files.

# Email from LDAP anonymous query
_GIT_SETUP_LDAP_HOST="${LDAP_URI:-ldaps://ldap1:636}"
_GIT_SETUP_LDAP_BASE="${LDAP_BASE_DN:-dc=docker,dc=internal}"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseDN is a problem here. We're allowing SSSD to do baseDN autodiscovery via rootDSE namingContext attribute. To match the SSSD config's baseDN, you would need to query the rootDSE, use namingContext if there's only one, otherwise use defaultNamingContext if theres multiple namingContexts otherwise fail (because SSSD would have failed too).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created some conditionals to resolve this issue.

# Resolve baseDN the same way SSSD does: rootDSE namingContexts autodiscovery.
# Use LDAP_BASE_DN if explicitly set; otherwise query rootDSE.
# - Single namingContexts entry  -> use it directly
# - Multiple namingContexts      -> use defaultNamingContext
# - Neither resolvable           -> abort
if [ -n "${LDAP_BASE_DN:-}" ]; then
    _GIT_SETUP_LDAP_BASE="$LDAP_BASE_DN"
else
    _GIT_SETUP_ROOTDSE=$(ldapsearch -x -H "$_GIT_SETUP_LDAP_HOST" -b "" -s base namingContexts defaultNamingContext 2>/dev/null)
    _GIT_SETUP_NC_COUNT=$(echo "$_GIT_SETUP_ROOTDSE" | grep -c '^namingContexts:')
    if [ "$_GIT_SETUP_NC_COUNT" -eq 1 ]; then
        _GIT_SETUP_LDAP_BASE=$(echo "$_GIT_SETUP_ROOTDSE" | awk '/^namingContexts:/{print $2; exit}')
    elif [ "$_GIT_SETUP_NC_COUNT" -gt 1 ]; then
        _GIT_SETUP_LDAP_BASE=$(echo "$_GIT_SETUP_ROOTDSE" | awk '/^defaultNamingContext:/{print $2; exit}')
    fi
    unset _GIT_SETUP_ROOTDSE _GIT_SETUP_NC_COUNT
fi
[ -z "${_GIT_SETUP_LDAP_BASE:-}" ] && return

command -v ldapsearch >/dev/null 2>&1 || return

# Skip if already configured — user-set values always take precedence
[ -n "$(git config --global user.email 2>/dev/null)" ] && return
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably only skip if user.email AND user.name are set globally.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to
[ -n "$(git config --global user.email 2>/dev/null)" ] && [ -n "$(git config --global user.name 2>/dev/null)" ] && return

[ -n "$(git config --global user.email 2>/dev/null)" ] && return

_GIT_SETUP_USER="${USER:-$(id -un 2>/dev/null)}"
[ -z "$_GIT_SETUP_USER" ] && return
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we bailout here as well if user is root? Just to avoid the unnessecary LDAP lookup?

Copy link
Copy Markdown
Collaborator Author

@cmyers-mieweb cmyers-mieweb Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_GIT_SETUP_USER="${USER:-$(id -un 2>/dev/null)}"
[ -z "$_GIT_SETUP_USER" ] && return
[ "$_GIT_SETUP_USER" = "root" ] && return


# Map LDAP cn attribute to the NSS gecos field so that tools like getent,
# finger, and the git-identity profile script can read the user's full name.
ldap_user_gecos = cn
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of this setting gecos works for our deployment. I don't want to complicate the sssd config more than nessecary.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and removed these lines

Install ldap-utils and add LDAP client/config and a profile script to auto-configure git user.name/email from LDAP on first interactive login. Copies ldap.conf to /etc/ldap, adds /etc/profile.d/git-identity.sh which uses ldapsearch and NSS (sssd) gecos to set global git config, and adjusts sssd.conf to map cn -> gecos (ldap_user_gecos = cn). Also updates Dockerfile to install ldap-utils and include the new files.
Enhance git-identity.sh to be more robust: only skip when both global user.name and user.email are set, ignore root, resolve LDAP baseDN via RootDSE (namingContexts / defaultNamingContext) when LDAP_BASE_DN isn't provided, and set user.name (from NSS gecos) and user.email (from LDAP) independently. Clean up temporary variables. Also remove the explicit ldap_user_gecos = cn mapping from sssd.conf since SSSD reads gecos by default; aligns git identity logic with SSSD behavior and handles multi-entry namingContexts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Containers should have git pre-configured

2 participants